<?php
$listHarga = $db->get_results('SELECT kode, jenis, nama, harga, satuan FROM `sem`.`harga` ORDER BY nama ASC', ARRAY_A);
	
$translator = array();
foreach($listHarga as $list){
	$translator['nama'][$list['kode']] 	 = $list['nama'];
	$translator['harga'][$list['kode']]  = $list['harga'];
	$translator['satuan'][$list['kode']] = $list['satuan'];
}

class MaterialFilterPost{
	public $material;
	public $peralatan;
	
	public function MaterialFilterPost($translator){
		$material  = array();
		$peralatan = array();
		
		// material
		foreach($_POST as $k=>$v){
			if( strpos($k, 'nama_material-') !== false ){
				$index = substr($k, strpos($k, '-')+1);
				
				$material['nama_material'][$index-1]   = $v;
				$material['jumlah_material'][$index-1] = (float)$_POST['jml_material-'.$index];
                
                if( isset($translator['satuan'][$v]) ){
                    $material['satuan_material'][$index-1] = $translator['satuan'][$v];
                }
                else{
                    $material['satuan_material'][$index-1] = $_POST['satuan_material-'.$index];
                }
				
                if( isset($translator['harga'][$v]) ){
                    $material['harga_material'][$index-1] = $translator['harga'][$v]*$material['jumlah_material'][$index-1];
                }
                else{
                    $material['harga_material'][$index-1] = (float)$_POST['harga_material-'.$index]*$material['jumlah_material'][$index-1];
                }
			}
		}		
		

		// peralatan
		foreach($_POST as $k=>$v){
			if( strpos($k, 'nama_peralatan-') !== false ){
				$index = substr($k, strpos($k, '-')+1);
				
				$peralatan['nama_peralatan'][$index-1]   = $v;
				$peralatan['jumlah_peralatan'][$index-1] = $_POST['jml_peralatan-'.$index];
			}
		}
		
		$this->material  = $material;
		$this->peralatan = $peralatan;
	}
}
?>